// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Parser functions

#include <cassert>

class Token;

#include "util/String.h"
#include "fe/ASTNode.h"
#include "fe/yyparse.h"
#include "fe/Parser.h"
#include "fe/Lexer2.h"
#include "fe/ParserException.h"
#include "fe/ParserFunctions.h"

void yyerror(Parser *parserContext, const char *msg)
{
	throw ParserException(String::fromUtf8(msg), parserContext->getScanner()->getCurrentLocation());
}

ASTNode *makeBinOpASTNode(ASTNode *lhs, int op, ASTNode *rhs)
{
	ASTNode *binOp = ASTNode::alloc(AST_BINARY_OP_EXPR, lhs->getLocation());
	binOp->setIntValue(op);
	binOp->appendChild(lhs);
	binOp->appendChild(rhs);
	return binOp;
}

const wchar_t *binOpToString(int op)
{
	const wchar_t *s = 0;
	
	switch (op) {
	case LOGICAL_AND:
		s = L"&&";
		break;
	case LOGICAL_OR:
		s = L"||";
		break;
	case EQUALS:
		s = L"==";
		break;
	case NOT_EQUALS:
		s = L"!=";
		break;
	case LESS_THAN:
		s = L"<";
		break;
	case LESS_THAN_EQ:
		s = L"<=";
		break;
	case GREATER_THAN:
		s = L">";
		break;
	case GREATER_THAN_EQ:
		s = L">=";
		break;
	case PLUS:
		s = L"+";
		break;
	case MINUS:
		s = L"-";
		break;
	case TIMES:
		s = L"*";
		break;
	case DIVIDE:
		s = L"/";
		break;
	default:
		assert(0);
	}
	
	return s;
}
